﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TfL
{
    //[Serializable]
    [XmlType("P")]
    public class Platform
    {
        public Platform()
        {
            
            Name = string.Empty;
            Code = 0;
            Trains = new List<Train>();
        }
        [XmlAttribute("N")]
        public string Name { get;  set; }
        
        [XmlAttribute("Code")]
        public int Code { get;  set; }

        [XmlArray("")]
        public List<Train> Trains { get; set; }

        public void Parse(XmlReader reader)
        {
            Code = int.Parse(reader.GetAttribute("Code"));
            Name = reader.GetAttribute("N");
            while (reader.Read())
            {
                if(reader.NodeType == XmlNodeType.EndElement && reader.Name=="P")
                    break;
                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "S")
                    break;
                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "T")
                    continue;
                if (reader.Name == "T")
                {
                    Train train = new Train();
                    train.Parse(reader);
                    Trains.Add(train);
                }
            }
        }

        public override string ToString()
        {
            return string.Format("{0}: {1} trains \n\r\t\t {2}", Name, Trains.Count, Trains.Aggregate("", (seed, platform) => seed + "\n\r\t\t" + platform));
        }
    }
}
